<template>
  <!--  公话- 统计报表-  充值统计报表 -->
  <div class="recharge_statistics_report">
    <el-form :inline="true" :model="elform" class="demo-form-inline" size="mini">
      <el-form-item label="开始日期:">
        <el-date-picker
          v-model="datetime[0]"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="开始日期"
          style="width: 130px"
          size="mini"
          :clearable="false"
        ></el-date-picker>
        <el-time-picker
          style="width:110px"
          v-model="time[0]"
          value-format="HH:mm:ss"
          placeholder="开始时间"
          :clearable="false"
          size="mini"
        ></el-time-picker>
      </el-form-item>
      <el-form-item label="结束日期:">
        <el-date-picker
          v-model="datetime[1]"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="结束日期"
          style="width: 130px"
          size="mini"
          :clearable="false"
        ></el-date-picker>
        <el-time-picker
          style="width:110px"
          v-model="time[1]"
          value-format="HH:mm:ss"
          placeholder="结束时间"
          :clearable="false"
          size="mini"
        ></el-time-picker>
      </el-form-item>
      <el-form-item label="充值类型:" >
        <el-select
          v-model="topuptype"
          placeholder="请选择"
          size="mini"
          clearable
          @clear="clearclass()"
          @change="chioceclass()"
          style="width:120px"
        >
          <el-option
            v-for="item in option"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="gosearch" size="mini" icon="el-icon-search">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="exportexeclto" size="mini">导出</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="dataListLoading"
      :data="tableinformation"
      style="width:100%"
      border="border"
      :header-cell-style="headFirst"
      highlight-current-row
      :cell-style="(res) => headFirst_col(res, [])"
    >
      <!-- <el-table-column prop="agentName" label="代理商名称"></el-table-column> -->
      <el-table-column prop="totalAmount" label="充值金额(元)">
        <template slot-scope="scope">
          <div>{{scope.row.totalAmount | amountjiao }}</div>
        </template>
      </el-table-column>
    </el-table>
    <div class="page">
      <el-pagination
        @size-change="handleSizeChangepage"
        @current-change="handleCurrentChangepage"
        :current-page="currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="recordCount"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      elform: {},
      option: [
        {
          value: '0',
          label: '平台充值'
        },
        {
          value: '1',
          label: '接口充值'
        }
      ],
      dataListLoading: false,
      pageNum: 1,
      pageSize: 20,
      recordCount: 0,
      currentPage: 1,
      topuptype: '',
      // 时间
      datetime: [],
      time: [],
      //学校id
      schoolid: '',
      //学校列表
      schooloptions: [],
      tableinformation: [],
      searchInfo: {},
      agentlistout: [],
      filename: '充值统计报表'
    }
  },
  created() {
    this.datetime = [this.getNowTime('yy-mm-dd'), this.getNowTime('yy-mm-dd')]
    this.time = ['00:00:00', '23:59:59']
  },
  computed: {
    changeschoolid: {
      get() {
        return this.$store.state.user.changeschoolid
      }
    }
  },
  methods: {
    gosearch() {
      this.pageNum = 1
      this.searchInfo = {}
      this.searchInfo.startDate = this.datetime[0] + ' ' + this.time[0]
      this.searchInfo.endDate = this.datetime[1] + ' ' + this.time[1]
      if (this.consumptiontype !== null && this.topuptype !== '') {
        this.searchInfo.consumptiontype = this.topuptype
      }
      this.searchInfo.unitGuid = this.changeschoolid
      this.topupstatistics()
    },
    exportexeclto() {
      if (this.tableinformation.length == 0) {
        this.$message({
          message: '查询数据后导出',
          type: 'warning'
        })
        return false
      }
      this.outexecel()
    },
    outexecel() {
      this.$http({
        url: this.$http.adornUrl('/voip/recharge/unit-summary'),
        method: 'post',
        data: this.$http.adornData({
          messageContent: {
            pageSize: this.recordCount,
            searchInfo: this.searchInfo
          },
          messageType: this.messageType
        })
      }).then(({ data }) => {
        if (data.messageCode === 200) {
          let jsonArr = data.messageContent.data
          this.agentlistout = jsonArr
          setTimeout(() => {
            this.export2Excel()
          }, 100)
        } else {
          this.$message.error('导出遇未知错误')
        }
      })
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },
    export2Excel() {
      const nowtime = this.getimeto()
      require.ensure([], () => {
        const multiHeader = []
        const multiHeader2 = []
        const merges = []
        const { export_json_to_excel } = require('../../../excel/Export2Excel')
        const tHeader = ['代理商名称', '充值金额(元)']
        const filterVal = ['agentName', 'totalAmount']
        const list = this.agentlistout
        const data = this.formatJson(filterVal, list)
        export_json_to_excel({
          multiHeader, //这里是第一行的表头
          multiHeader2, //这里是第二行的表头
          header: tHeader, //这里应该是算第三行的表头
          data,
          merges,
          filename: this.filename + nowtime
        })
      })
    },
    handleSizeChangepage(val) {
      this.pageSize = val
      this.topupstatistics()
    },
    handleCurrentChangepage(val) {
      this.pageNum = val
      this.topupstatistics()
    },
    //充值统计列表
    topupstatistics() {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/voip/recharge/unit-summary'),
        method: 'post',
        data: this.$http.adornData({
          messageContent: {
            pageNum: this.pageNum,
            pageSize: this.pageSize,
            searchInfo: this.searchInfo
          },
          messageType: 0
        })
      }).then(({ data }) => {
        this.dataListLoading = false
        if (data.messageCode === 200) {
          this.tableinformation = data.messageContent.data || []
          this.recordCount = data.messageContent.recordCount
          this.currentPage = data.messageContent.pageNum
            if (this.tableinformation.length == 0) {
            this.$message({
              message: '查询成功当前暂无数据',
              type: 'success'
            })
          }
        } else {
          this.$message.error(data.errorMessage)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.recharge_statistics_report {
  padding: 10px;
  .demo-form-inline,
  .demo-form-inline1 {
    /deep/.el-form-item__label {
      padding-right: 5px;
    }
    .el-form-item {
      margin-bottom: 5px;
    }
    .el-date-editor.el-input,
    .el-date-editor.el-input__inner {
      width: 180px;
    }
  }
  .page {
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>